package com.ljc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljc.entity.*;
import com.ljc.service.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * 提供接口服务
 *
 * @author ljc
 */
@RestController
@RequestMapping("/interface")
public class interfaceController {
    @Resource
    private WallpaperService wallpaperService;
    @Resource
    private SentenceService sentenceService;
    @Resource
    private JokeService jokeService;
    @Resource
    private TakeOutService takeOutService;
    @Resource
    private AvatarService avatarService;

    /**
     * 使用post方式获取壁纸
     */
    @PostMapping("/wallpaper")
    public String getUsernameByPost(@RequestBody Wallpaper wallpaper) {
        String tag = wallpaper.getTag();
        QueryWrapper<Wallpaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag", tag);
        List<Wallpaper> wallpaperList = wallpaperService.list(queryWrapper);
        if (CollectionUtils.isEmpty(wallpaperList)) {
            return "暂不支持该种类别的壁纸";
        }
        int count = wallpaperList.size();
        Wallpaper paper = wallpaperList.get(new Random().nextInt(count));
        return paper.getUrl();
    }

    /**
     * 获取心灵鸡汤
     */
    @GetMapping("/sentence")
    public String getMotivationalSentence() {
        int count = (int) sentenceService.count();
        Sentence randomSentence = sentenceService.getById(new Random().nextInt(count) + 1);
        return randomSentence.getSentence();
    }

    /**
     * 获取随机头像
     */
    @GetMapping("/avatar")
    public String getAvatar() {
        int count = (int) avatarService.count();
        Avatar randomAvatar = avatarService.getById(new Random().nextInt(count) + 1);
        return randomAvatar.getAvatar();
    }

    /**
     * 获取笑话
     */
    @GetMapping("/joke")
    public String getJoke() {
        int count = (int) jokeService.count();
        Joke randomJoke = jokeService.getById(new Random().nextInt(count) + 1);
        return randomJoke.getJoke();
    }

    /**
     * 获取外卖种类
     */
    @GetMapping("/takeout")
    public String getTakeOut() {
        int count = (int) takeOutService.count();
        TakeOut randomTakeOut = takeOutService.getById(new Random().nextInt(count) + 1);
        return randomTakeOut.getTakeOut();
    }
}
